package sk.ygor.raynor.client.json.basetype;

import com.google.gwt.json.client.JSONNull;
import com.google.gwt.json.client.JSONValue;
import sk.ygor.raynor.client.json.JSONSerializer;

public abstract class BaseTypeSerializer<T> implements JSONSerializer<T>, JSONSerializer.Configuration {

    @Override
    public void setTransformer(JSONSerializer.Transformer transformer) {
        // do nothing
    }

    public final T deserialize(JSONValue jsonValue) {
        if (/*jsonValue == null || */jsonValue.isNull() != null) {
            return null;
        } else {
            return deserializeNonNullValue(jsonValue);
        }
    }

    protected abstract T deserializeNonNullValue(JSONValue jsonValue);

    public final JSONValue serialize(T javaValue) {
        return javaValue == null ? JSONNull.getInstance() : serializeNonNullValue(javaValue);
    }

    protected abstract JSONValue serializeNonNullValue(T javaValue);

}
